package com.example.demo.controller;

import com.example.demo.common.Func;
import com.example.demo.common.Result;
import com.example.demo.common.constant.RuleCode;
import com.example.demo.verify.ParamValid;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.pdf.PDFParser;
import org.apache.tika.parser.txt.TXTParser;
import org.apache.tika.sax.BodyContentHandler;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.xml.sax.SAXException;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/tika")
@Api(tags = "16、文档解析")
@ApiSupport(author = "罗海林", order = 16)
@Validated
@RequiredArgsConstructor
public class TikaController {

    @PostMapping("/analysisDocx")
    @ApiOperation(value = "解析docx文档", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 1)
    public Result analysisDocx() throws IOException, TikaException {
        Tika tika = new Tika();

        String path = Func.getRootPath() + "/file/upload/mfkj.docx";
        InputStream stream = new FileInputStream(path);
        String result = tika.parseToString(stream);

        Result res = new Result();
        res.ok();
        res.put("info", result);
        return res;
    }

    @PostMapping("/analysisTxt")
    @ApiOperation(value = "解析txt文档", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 2)
    public Result analysisTxt() throws IOException, TikaException, SAXException {
        TXTParser parser = new TXTParser();
        BodyContentHandler handler = new BodyContentHandler();
        Metadata metadata = new Metadata();
        ParseContext context = new ParseContext();

        String path = Func.getRootPath() + "/file/upload/mfkj.txt";
        InputStream stream = new FileInputStream(path);
        parser.parse(stream, handler, metadata, context);

        Result res = new Result();
        res.ok();
        res.put("handler", handler.toString());
        res.put("metadata", metadata.toString());
        return res;
    }

    @PostMapping("/analysisPdf")
    @ApiOperation(value = "解析pdf文档", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 3)
    public Result analysisPdf() throws IOException, TikaException, SAXException {
        PDFParser parser = new PDFParser();
        BodyContentHandler handler = new BodyContentHandler();
        Metadata metadata = new Metadata();
        ParseContext context = new ParseContext();

        String path = Func.getRootPath() + "/file/upload/mfkj.pdf";
        InputStream stream = new FileInputStream(path);
        parser.parse(stream, handler, metadata, context);

        Result res = new Result();
        res.ok();
        res.put("handler", handler.toString());
        res.put("metadata", metadata.toString());
        return res;
    }

    @PostMapping("/analysisAuto")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "suffix", value = "文档后缀名")
    })
    @ApiOperation(value = "自动识别解析文档", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 4)
    public Result analysisAuto(@ParamValid(name = "文档后缀名", rule = RuleCode.noEmpty) @RequestParam String suffix) throws IOException, TikaException, SAXException {
        AutoDetectParser parser = new AutoDetectParser();
        BodyContentHandler handler = new BodyContentHandler();
        Metadata metadata = new Metadata();

        String path = Func.getRootPath() + "/file/upload/mfkj." + suffix;
        InputStream stream = new FileInputStream(path);
        parser.parse(stream, handler, metadata);

        Result res = new Result();
        res.ok();
        res.put("handler", handler.toString());
        res.put("metadata", metadata.toString());
        return res;
    }

    /*@PostMapping("/analysisPic")
    @ApiOperation(value = "识别图片文字", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 5)
    public Result analysisPic() throws TesseractException {
        String path = Func.getRootPath() + "/file/upload/1111.png";
        File image = new File(path);

        Tesseract tesseract = new Tesseract();
        tesseract.setDatapath("src/main/resources/tessdata");
        tesseract.setLanguage("chi_sim");

        tesseract.setPageSegMode(1);
        tesseract.setOcrEngineMode(1);

        String result = tesseract.doOCR(image);

        Result res = new Result();
        res.ok();
        res.put("info", result);
        return res;
    }*/
}
